package com.pan.hisystem.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.pan.hisystem.pojo.Outpatients;
import com.pan.hisystem.pojo.PatientTest;
import com.pan.hisystem.pojo.Prescription;
import com.pan.hisystem.pojo.RespBean;
import com.pan.hisystem.service.IOutpatientsService;
import com.pan.hisystem.service.IPatientTestService;
import com.pan.hisystem.service.IPrescriptionService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Description 划价收费
 * @Author qingyang
 * @Date 2022/3/24 21:38
 * @Version 1.0
 **/
@RestController
@RequestMapping("/charge")
public class ChargeController {

    @Autowired
    private IPrescriptionService prescriptionService;
    @Autowired
    private IOutpatientsService outpatientsService;
    @Autowired
    private IPatientTestService patientTestService;


    @ApiOperation(value = "获取所有处方")
    @GetMapping("/getAllPrescriptions")
    public List<Prescription> getAllPrescriptions(){
        return prescriptionService.list();
    }

    @ApiOperation(value = "获取根据处方号获取药品")
    @GetMapping("/getDrugsByPreId")
    public RespBean getDrugsByPreId(Integer preId){
        return prescriptionService.getDrugsByPreId(preId);
    }

    @ApiOperation(value = "获得处方总金额")
    @GetMapping("/getPrice")
    public RespBean getPrice(Integer preId){
        return prescriptionService.getPrice(preId);
    }

    @ApiOperation(value = "缴费")
    @PutMapping("/updatePrescription")
    public RespBean updatePrescription(@RequestBody Prescription prescription){
        return prescriptionService.updatePrescription(prescription);
    }

    @ApiOperation(value = "根据卡号获取患者门诊收费信息记录")
    @GetMapping("/getChargeMes")
    public List<Prescription> getChargeMes(String mid,Boolean payStatus){
        return prescriptionService.getChargeMes(mid,payStatus);
    }
    @ApiOperation(value = "根据卡号获取患者门诊收费信息")
    @GetMapping("/getMes")
    public List<Prescription> getMes(String mid,Boolean payStatus){
        return prescriptionService.getMes(mid,payStatus);
    }

    @ApiOperation(value = "退药")
    @GetMapping("/backDrugs")
    public RespBean backDrugs(Integer id){
        return prescriptionService.backDrugs(id);
    }

    @ApiOperation(value = "根据卡号获取患者化验收费信息")
    @GetMapping("/getTestByMid")
    public List<Outpatients> getTestByMid(String mid){
        return outpatientsService.getTestByMid(mid);
    }

    @ApiOperation(value = "化验缴费")
    @PutMapping("/updateTest")
    public RespBean updateTest(@RequestBody PatientTest patientTest){
        System.out.println(patientTest.toString());
        if(patientTest.getId()==null){
            return RespBean.error("缴费失败!");
        }
        if(patientTest.getPayStatus()){
            return RespBean.error("该化验项目已缴费!无需重复缴费");
        }
        patientTest.setPayStatus(true);
        patientTest.setPayTime(LocalDateTime.now());
        if(patientTestService.updateById(patientTest)){
            return RespBean.success("缴费成功!");
        }
        return RespBean.error("缴费失败!");
    }

    @ApiOperation(value = "退化验费")
    @PutMapping("/backTest")
    public RespBean backTest(Integer id){
        System.out.println(id);
        if(id==null){
            return RespBean.error("退费失败!");
        }
        if(patientTestService.update(new UpdateWrapper<PatientTest>()
                .set("back_status",true)
                .eq("pay_status",true)
                .eq("status",false)
                .eq("id",id))){
            return RespBean.success("退费成功!");
        }
        return RespBean.error("退费失败!");
    }




}
